/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

/**
 *
 * @author rao
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import util.CalSerializer;
import vo.Task;

public class TaskManagerTcpClient {

	private static Socket socket;
	private static DataOutputStream dos;
	private static DataInputStream dis;


	public static void main(String args[])  {
		try {
			// IP address of the server,
			InetAddress serverAddress = InetAddress.getByName("localhost");

			// It is the same port where server will be listening.
			int serverPort = 7896;

			// Open a socket for communication.
			socket = new Socket(serverAddress, serverPort);

			// Get data output stream to send a String message to server.
			dos = new DataOutputStream(socket.getOutputStream());

			// Now switch to listening mode for receiving message from server.
			dis = new DataInputStream(socket.getInputStream());

			Task newTask = new Task("task4", "a test Task", "13-11-2012", "a task for testing client-server", "jaje");

			// Test GET Command
			System.out.println( get("jaje"));

			System.out.println( post(newTask) );

			System.out.println( put(newTask) );

			//delete("test");

			//exit and close
			close();




		} catch (IOException ex) {
			Logger.getLogger(TaskManagerTcpClient.class.getName()).log(Level.SEVERE, null, ex);

			System.out.println("error message: " + ex.getMessage());
		} 

	}

	private static String get(String userId) {

		String message = "GET"; String response;

		try {

			dos.writeUTF(message);
			dos.flush();
			response = dis.readUTF();

			if ( response.equalsIgnoreCase("get") ) {

				dos.writeUTF(userId);
				dos.flush();

				//returns TaskList
				message = CalSerializer.unMarshallTaskList( dis.readUTF()).toString();	

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return message;
	}

	private static String post(Task newTask) {

		String message = "POST"; String response;

		try {

			dos.writeUTF(message);
			dos.flush();
			response = dis.readUTF();

			if ( response.equalsIgnoreCase("post") ) {

				// TODO
				dos.writeUTF(CalSerializer.marshallTask(newTask));
				dos.flush();
				message = dis.readUTF();

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return message;

	}

	private static String put(Task task) {

		String message = "PUT"; String response;

		try {

			dos.writeUTF(message);
			dos.flush();
			response = dis.readUTF();

			if ( response.equalsIgnoreCase("put") ) {

				//TODO send task
				dos.writeUTF(CalSerializer.marshallTask(task));
				dos.flush();
				message = dis.readUTF();

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return message;
	}

	private static String delete(String tId) {

		String message = "DELETE"; 
		String response;

		try {

			dos.writeUTF(message);
			dos.flush();
			response = dis.readUTF();

			if ( response.equalsIgnoreCase("delete") ) {

				//TODO
				dos.writeUTF("task");
				dos.flush();

				message = dis.readUTF();

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return message;

	}

	public static void close() {

		// exit and close socket
		String message = "exit";
		try {
			dos.writeUTF(message);
			dos.flush();
			String response = dis.readUTF();
			System.out.println("Message from Server: " + response );
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


}





